package com.grace.law.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.grace.law.bean.Case;
import com.grace.law.bean.CaseRemark;
import com.grace.law.bean.Event;
import com.grace.law.bean.Msg;
import com.grace.law.service.CaseService;
import com.grace.law.service.EventTaskService;

@Controller
@RequestMapping("/caseremark")
public class CaseRemarkController {
	@Autowired
	CaseService caseService;
	@Autowired
	EventTaskService taskService;
	
	@RequestMapping(value = "/init/{customerId}",method=RequestMethod.GET)
	public ModelAndView edit(@PathVariable("customerId") String customerId) {
		Case data = caseService.selectCaseById(customerId);
		ModelAndView mv = new ModelAndView("caseremark");
		mv.addObject("customer", data);
		return mv;

	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Msg getCasesWithJson(@RequestParam(value = "customerId") Integer customerId) {
		List<CaseRemark> remarks = caseService.getRemarkByCustomerId(customerId);
		
		return Msg.success().add("remarks", remarks);
	}
	
	@RequestMapping(value = "/getremark/{rid}",method=RequestMethod.GET)
	@ResponseBody
	public Msg getRemark(@PathVariable(value = "rid") Integer rid) {
		CaseRemark remarks = caseService.getRemark(rid);
		
		return Msg.success().add("remarks", remarks);
	}
	
	@RequestMapping(value = "/input", method = RequestMethod.POST)
	@ResponseBody
	public Msg submit(CaseRemark record) {
		Date now= new Date();
		record.setCreateTime(now);
		int result = caseService.insert(record);
		if(result<0){
			Event event = new Event();
			event.setCustomerId(record.getCustomerId());
			event.setEventTouchTime(record.getNextContactTime());
			event.setCreateTime(now);
			event.setEventType("000002000001");
			taskService.insert(event);
		}

		return Msg.success();

	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Msg save(CaseRemark record) {
		if(caseService.save(record)<0){
			return Msg.success();
		}

		return Msg.error();
	}
	
	
}
